<template>
  <div class="page">
    <h2>神经调控 — 适应症评估与个体化方案</h2>
    <div class="controls">
      <el-input v-model="q" placeholder="按患者姓名或诊断搜索" clearable style="flex:1" />
      <el-select v-model="filterModality" placeholder="刺激方式" style="width:160px">
        <el-option label="全部" :value="''" />
        <el-option label="rTMS" value="rTMS" />
        <el-option label="tDCS" value="tDCS" />
        <el-option label="DBS" value="DBS" />
      </el-select>
      <el-button type="primary" @click="openForm()">新建评估</el-button>
    </div>

    <div class="muted">参考适应症与注意事项：治疗难治性抑郁症（rTMS/iTBS）、强迫症（DBS/rTMS）、慢性疼痛/幻听（rTMS）、慎用金属植入/癫痫病史、合并用药注意降低阈值药物等。</div>

    <el-table :data="filtered" stripe border style="width:100%;margin-top:12px">
      <el-table-column prop="patient" label="患者" width="160" />
      <el-table-column prop="age" label="年龄" width="80" />
      <el-table-column prop="diagnosis" label="主要诊断" width="220" />
      <el-table-column prop="modality" label="建议方式" width="120" />
      <el-table-column prop="recommendation" label="初步方案摘要" />
      <el-table-column label="操作" width="220">
        <template #default="{ row }">
          <el-button size="small" @click="openForm(row.id)">查看/编辑</el-button>
          <el-button size="small" type="warning" @click="generatePlan(row.id)" style="margin-left:8px">生成个体化方案</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="适应症评估表" :model-value="showForm" width="820px" @close="closeForm">
      <el-form :model="form" label-width="140px">
        <el-form-item label="患者姓名">
          <el-input v-model="form.patient" />
        </el-form-item>
        <el-form-item label="年龄/性别">
          <el-input v-model="form.age" style="width:120px;margin-right:8px" />
          <el-select v-model="form.gender" placeholder="性别" style="width:120px">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
            <el-option label="其他" value="其他" />
          </el-select>
        </el-form-item>
        <el-form-item label="主要诊断/症状">
          <el-input v-model="form.diagnosis" placeholder="例如：重度抑郁症，抗药" />
        </el-form-item>
        <el-form-item label="重要既往史/禁忌">
          <el-input type="textarea" v-model="form.history" placeholder="如：癫痫、颅内金属、心脏起搏器等" />
        </el-form-item>
        <el-form-item label="当前用药/可能影响阈值的药物">
          <el-input type="textarea" v-model="form.medications" placeholder="例如：氯吡格雷、三环类、抗惊厥药等" />
        </el-form-item>
        <el-form-item label="评估量表分数">
          <el-input v-model="form.scales" placeholder="例如：HAM-D 24；PHQ-9 18" />
        </el-form-item>
        <el-form-item label="首选刺激方式">
          <el-select v-model="form.modality" placeholder="选择方式" style="width:220px">
            <el-option label="rTMS" value="rTMS" />
            <el-option label="iTBS" value="iTBS" />
            <el-option label="tDCS" value="tDCS" />
            <el-option label="DBS" value="DBS" />
          </el-select>
        </el-form-item>
        <el-form-item label="临床备注/目标">
          <el-input type="textarea" v-model="form.notes" placeholder="短期目标/长期期望" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="closeForm">取消</el-button>
        <el-button type="primary" @click="saveAssessment">保存评估</el-button>
      </template>
    </el-dialog>

    <el-dialog title="个体化方案（草案）" :model-value="showPlan" width="760px" @close="showPlan=false">
      <div v-if="currentPlan">
        <el-descriptions title="方案摘要" :column="1" border>
          <el-descriptions-item label="患者">{{currentPlan.patient}}</el-descriptions-item>
          <el-descriptions-item label="建议方式">{{currentPlan.modality}}</el-descriptions-item>
          <el-descriptions-item label="目标">{{currentPlan.goal}}</el-descriptions-item>
          <el-descriptions-item label="主要参数">{{currentPlan.paramsSummary}}</el-descriptions-item>
        </el-descriptions>
        <div style="margin-top:12px">
          <h4>详细参数与随访计划</h4>
          <el-table :data="currentPlan.details" style="width:100%" size="small" border>
            <el-table-column prop="k" label="项目" width="180" />
            <el-table-column prop="v" label="内容" />
          </el-table>
        </div>
      </div>
      <template #footer>
        <el-button @click="showPlan=false">关闭</el-button>
        <el-button type="primary" @click="applyPlan">保存并应用</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue';
import { ElMessage } from 'element-plus';

const DEFAULT = [
  { id:1, patient:'王小明', age:34, gender:'男', diagnosis:'重度抑郁（抗药）', modality:'iTBS', recommendation:'左DLPFC iTBS 600 pulses/天，6周', goal:'抑郁量表下降≥50%', paramsSummary:'iTBS 左DLPFC 600 pulses/day 80%MT', details:[] },
  { id:2, patient:'刘女士', age:46, gender:'女', diagnosis:'难治性强迫症，考虑DBS', modality:'DBS', recommendation:'转诊神经外科评估，靶点 SCC/ventral capsule', goal:'强迫症量表显著下降', paramsSummary:'DBS 个体化电极定位', details:[] }
];

const data = ref(JSON.parse(JSON.stringify(DEFAULT)));
const q = ref('');
const filterModality = ref('');

const showForm = ref(false);
const showPlan = ref(false);
const editingId = ref(null);
const form = reactive({ patient:'', age:'', gender:'男', diagnosis:'', history:'', medications:'', scales:'', modality:'rTMS', notes:'' });
const currentPlan = ref(null);

const filtered = computed(()=>{
  const qq = q.value.trim().toLowerCase();
  return data.value.filter(d=>{
    if(qq && !(d.patient.toLowerCase().includes(qq) || (d.diagnosis||'').toLowerCase().includes(qq))) return false;
    if(filterModality.value && d.modality !== filterModality.value) return false;
    return true;
  });
});

function openForm(id){ editingId.value = id || null; if(id){ const it = data.value.find(x=>x.id===id); if(it) Object.assign(form, JSON.parse(JSON.stringify(it))); } else { Object.assign(form,{patient:'',age:'',gender:'男',diagnosis:'',history:'',medications:'',scales:'',modality:'rTMS',notes:''}); } showForm.value = true; }
function closeForm(){ showForm.value = false; editingId.value = null; }
function saveAssessment(){ if(!form.patient || !form.diagnosis){ ElMessage.warning('患者姓名与主要诊断为必填'); return; } if(editingId.value){ const it = data.value.find(x=>x.id===editingId.value); if(it) Object.assign(it, JSON.parse(JSON.stringify(form))); } else { const nid = (data.value.reduce((s,i)=>Math.max(s,i.id),0) || 0) + 1; data.value.push(Object.assign({id:nid, recommendation:'', goal:'', paramsSummary:'', details:[]}, JSON.parse(JSON.stringify(form)))); }
  showForm.value=false; ElMessage.success('已保存评估'); }

function generatePlan(id){ const it = data.value.find(x=>x.id===id); if(!it) return; // 简单规则引擎示例
  const plan = { patient: it.patient, modality: it.modality || 'rTMS', goal:'改善核心症状，观察量表变化', paramsSummary:'根据指南与文献拟定参数', details:[] };
  if(plan.modality==='iTBS'){
    plan.paramsSummary = 'iTBS 左DLPFC 600 pulses/日，80% RMT，每日一次，持续4-6周（初评6周）';
    plan.details = [
      {k:'目标靶区', v:'左DLPFC (F3) 或经影像定位'},
      {k:'强度', v:'80% RMT'},
      {k:'频率/模式', v:'iTBS（短程重复Theta burst）'},
      {k:'每次脉冲数', v:'600 脉冲'},
      {k:'每周次数', v:'5 次/周'},
      {k:'总疗程', v:'20-30 次（4-6 周）'},
      {k:'随访', v:'基线、2 周、4 周、6 周 评估量表与不良事件'},
    ];
  } else if(plan.modality==='rTMS'){
    plan.paramsSummary = '左DLPFC 高频10Hz 3000 pulses/次，或右DLPFC 1Hz 抑制方案；80-120% RMT';
    plan.details = [
      {k:'目标靶区', v:'左/右 DLPFC'},
      {k:'强度', v:'80-120% RMT (依患者耐受)'} ,
      {k:'频率', v:'10 Hz (激活) / 1 Hz (抑制)'},
      {k:'每次脉冲数', v:'2000-3000'},
      {k:'每周次数', v:'5 次/周'},
      {k:'总疗程', v:'4-6 周'},
      {k:'随访', v:'2、4、8 周 评估；监测头痛、眩晕、局灶不适，注意癫痫风险'},
    ];
  } else if(plan.modality==='tDCS'){
    plan.paramsSummary = '常用 1-2 mA，20 分钟，阳极置左DLPFC，阴极对侧';
    plan.details = [
      {k:'电流强度', v:'1-2 mA'},
      {k:'时间', v:'20-30 分钟/次'},
      {k:'电极位', v:'阳极左DLPFC，阴极右上肩/对侧顶叶'},
      {k:'频次', v:'每天或隔日，疗程 2-4 周'},
      {k:'随访', v:'皮肤刺激、头痛、感觉异常监测'}
    ];
  } else if(plan.modality==='DBS'){
    plan.paramsSummary = '需神经外科会诊，电极定位与参数个体化，术后编程逐步调整';
    plan.details = [
      {k:'评估', v:'详细神经外科影像与多学科会诊'},
      {k:'术后编程', v:'起始电流/频率/脉冲宽度由团队调整'},
      {k:'随访', v:'术后短期及长期随访，硬件管理'}
    ];
  }
  currentPlan.value = plan; showPlan.value = true;
}

function applyPlan(){ // 简单示例：将方案摘要写入记录
  if(!currentPlan.value) return; const it = data.value.find(x=>x.patient===currentPlan.value.patient); if(it){ it.recommendation = currentPlan.value.paramsSummary; it.goal = currentPlan.value.goal; it.paramsSummary = currentPlan.value.paramsSummary; it.details = currentPlan.value.details; }
  showPlan.value=false; ElMessage.success('个体化方案已生成并保存为草案'); }
</script>

<style scoped>
.page{font-family: Arial, "Microsoft YaHei", sans-serif; margin:10px;}
h2{margin-top:-10px}
.controls{display:flex;gap:8px;margin-bottom:12px;align-items:center}
.muted{color:#666;font-size:13px}
</style>
